package com.alibaba.itu.tala.ui.perspective;

import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.progress.IProgressConstants;

import com.alibaba.itu.tala.ReviewPlugin;
import com.alibaba.itu.tala.ui.view.editor.ReviewEditorView;
import com.alibaba.itu.tala.ui.view.list.ReviewListView;
import com.alibaba.itu.tala.ui.view.svn.HistoryListView;
import com.alibaba.itu.tala.util.TalaLogger;

public class ReviewPerspective implements IPerspectiveFactory {

    // jupiter perspective ID.
    public static final String      ID  = "com.alibaba.itu.tala.ui.perspective";
    private static final TalaLogger log = TalaLogger.getLogger();

    /**
     * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout)
     */
    public void createInitialLayout(IPageLayout layout) {
        defineDefaultLayout(layout);
    }

    /**
     * Defines layout.
     * 
     * @param layout the layout.
     */
    private void defineDefaultLayout(IPageLayout layout) {
        String editorArea = layout.getEditorArea();
        IFolderLayout bottom = layout.createFolder("bottom", IPageLayout.BOTTOM, (float) 0.70, editorArea);
        bottom.addView(ReviewListView.VIEW_ID);
        bottom.addView(ReviewEditorView.EDITOR_VIEW_ID);
        bottom.addView(HistoryListView.VIEW_ID);
        bottom.addView(IConsoleConstants.ID_CONSOLE_VIEW);
        bottom.addPlaceholder(IPageLayout.ID_PROBLEM_VIEW);
        bottom.addPlaceholder(IProgressConstants.PROGRESS_VIEW_ID);
        bottom.addPlaceholder(NewSearchUI.SEARCH_VIEW_ID);

        IFolderLayout folder = layout.createFolder("topLeft", IPageLayout.LEFT, (float) 0.25, editorArea);
        //folder.addView(JavaUI.ID_PACKAGES);
        //folder.addView(IPageLayout.ID_PROJECT_EXPLORER);
        folder.addView("org.eclipse.jdt.ui.PackageExplorer");
        folder.addView("org.eclipse.ui.navigator.ProjectExplorer");

        layout.addView(IPageLayout.ID_OUTLINE, IPageLayout.RIGHT, (float) 0.75, editorArea);
        layout.addActionSet(IDebugUIConstants.LAUNCH_ACTION_SET);
        layout.addActionSet(IPageLayout.ID_NAVIGATE_ACTION_SET);
    }

    /**
     * Switches to the review perspective.
     */
    public static void showPerspective() {
        IWorkbench workbench = ReviewPlugin.getDefault().getWorkbench();
        IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
        if (window != null) {
            try {
                workbench.showPerspective(ReviewPerspective.ID, window);
            } catch (WorkbenchException e) {
                log.error(e);
            }
        }
    }
}
